package your_car_status

import org.springframework.dao.DataIntegrityViolationException

class AdminController {
    
    def newRepairShop(){
        [repairShopInstance:new RepairShop()]
    }
    
    def listRepairShop(){
        [repairShopInstanceList: RepairShop.list()]
    }
    
    def saveRepairShop(){
        def repairShopInstance = new RepairShop(params)
        if (!repairShopInstance.save(flush: true)) {
            render(view: "newRepairShop", model: [repairShopInstance: repairShopInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), repairShopInstance.id])
        redirect(action: "newRepairShop", id: repairShopInstance.id)
    }
    
    def editRepairShop(Long id){
        def repairShopInstance = RepairShop.get(id)
        if (!repairShopInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), id])
            redirect(action: "listRepairShop")
            return
        }

        [repairShopInstance: repairShopInstance]
    }
    
    def updateRepairShop(Long id, Long version) {
        def repairShopInstance = RepairShop.get(id)
        if (!repairShopInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (repairShopInstance.version > version) {
                repairShopInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'repairShop.label', default: 'RepairShop')] as Object[],
                          "Another user has updated this RepairShop while you were editing")
                render(view: "editRepairShop", model: [repairShopInstance: repairShopInstance])
                return
            }
        }

        repairShopInstance.properties = params

        if (!repairShopInstance.save(flush: true)) {
            render(view: "editRepairShop", model: [repairShopInstance: repairShopInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'repairShop.label', default: 'RepairShop'), repairShopInstance.id])
        redirect(action: "editRepairShop", id: repairShopInstance.id)
    }
    
    def newCustomer(){
        [customerInstance: new Customer()]
    }
    
    def listCustomer(){
        [customerInstanceList: Customer.list()]
    }
    
    def saveCustomer(){
        def customerInstance = new Customer(params)
        if (!customerInstance.save(flush: true)) {
            render(view: "newCustomer", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "newCustomer", id: customerInstance.id)
    }
    
    def editCustomer(Long id){
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "listCustomer")
            return
        }

        [customerInstance: customerInstance]
    }
    
    def updateCustomer(Long id, Long version) {
        def customerInstance = Customer.get(id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (customerInstance.version > version) {
                customerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'customer.label', default: 'Customer')] as Object[],
                          "Another user has updated this Customer while you were editing")
                render(view: "editCustomer", model: [customerInstance: customerInstance])
                return
            }
        }

        customerInstance.properties = params

        if (!customerInstance.save(flush: true)) {
            render(view: "editCustomer", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "editCustomer", id: customerInstance.id)
    }
    
    def newMechanicalTechnician(){
        [mechanicalTechnicianInstance:new MechanicalTechnician()]
    }
    
    def listMechanicalTechnician(){
        [mechanicalTechnicianInstanceList: MechanicalTechnician.list()]
    }
    
    def saveMechanicalTechnician(){
        def mechanicalTechnicianInstance = new MechanicalTechnician(params)
        if (!mechanicalTechnicianInstance.save(flush: true)) {
            render(view: "newMechanicalTechnician", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), mechanicalTechnicianInstance.id])
        redirect(action: "newMechanicalTechnician", id: mechanicalTechnicianInstance.id)
    }
    
    def editMechanicalTechnician(Long id){
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(action: "listMechanicalTechnician")
            return
        }

        [mechanicalTechnicianInstance: mechanicalTechnicianInstance]
    }
    
    def updateMechanicalTechnician(Long id, Long version) {
        def mechanicalTechnicianInstance = MechanicalTechnician.get(id)
        if (!mechanicalTechnicianInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (mechanicalTechnicianInstance.version > version) {
                mechanicalTechnicianInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician')] as Object[],
                          "Another user has updated this MechanicalTechnician while you were editing")
                render(view: "editMechanicalTechnician", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance])
                return
            }
        }

        mechanicalTechnicianInstance.properties = params

        if (!mechanicalTechnicianInstance.save(flush: true)) {
            render(view: "editMechanicalTechnician", model: [mechanicalTechnicianInstance: mechanicalTechnicianInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'mechanicalTechnician.label', default: 'MechanicalTechnician'), mechanicalTechnicianInstance.id])
        redirect(action: "editMechanicalTechnician", id: mechanicalTechnicianInstance.id)
    }
    
    def newVehicle(){
        [vehicleInstance:new Vehicle()]
    }
    
    def listVehicle(){
        [vehicleInstanceList: Vehicle.list()]
    }
    
    def searchVehicle (String licensePlate){
        def vehicleInstance = Vehicle.findAllByLicensePlate(licensePlate.toUpperCase())
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), licensePlate])
            return
        }
        [vehicleInstanceList: vehicleInstance]
    }
    
    def saveVehicle(){
        def vehicleInstance = new Vehicle(params)
        if (!vehicleInstance.save(flush: true)) {
            render(view: "newVehicle", model: [vehicleInstance: vehicleInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), vehicleInstance.id])
        redirect(action: "newVehicle", id: vehicleInstance.id)
    }
    
    def editVehicle(Long id){
        def vehicleInstance = Vehicle.get(id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), id])
            redirect(action: "listVehicle")
            return
        }

        [vehicleInstance: vehicleInstance]
    }
    
    def updateVehicle(Long id, Long version) {
        def vehicleInstance = Vehicle.get(id)
        if (!vehicleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (vehicleInstance.version > version) {
                vehicleInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'vehicle.label', default: 'Vehicle')] as Object[],
                          "Another user has updated this Vehicle while you were editing")
                render(view: "editVehicle", model: [vehicleInstance: vehicleInstance])
                return
            }
        }

        vehicleInstance.properties = params

        if (!vehicleInstance.save(flush: true)) {
            render(view: "editVehicle", model: [vehicleInstance: vehicleInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'vehicle.label', default: 'Vehicle'), vehicleInstance.id])
        redirect(action: "editVehicle", id: vehicleInstance.id)
    }

    def listDiagnostic(Long id) {
        def veh=Vehicle.get(id).diagnostic;
        [diagnosticInstanceList: veh, diagnosticInstanceTotal: Diagnostic.count(),carId:id]
    }
    
    def newDiagnostic() {
        [diagnosticInstance: new Diagnostic(params)]
    }

    def saveDiagnostic(){
        def diagnosticInstance = new Diagnostic(params)
        if (!diagnosticInstance.save(flush: true)) {
            render(view: "newDiagnostic", model: [diagnosticInstance: diagnosticInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), diagnosticInstance.id])
        redirect(action: "newDiagnostic", id: diagnosticInstance.id)
    }
    
    def editDiagnostic(Long id) {
        def diagnosticInstance = Diagnostic.get(id)
        if (!diagnosticInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "editDiagnostic")
            return
        }

        [diagnosticInstance: diagnosticInstance]
    }

    def updateDiagnostic(Long id, Long version) {
        def diagnosticInstance = Diagnostic.get(id)
        if (!diagnosticInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "editDiagnostic")
            return
        }

        if (version != null) {
            if (diagnosticInstance.version > version) {
                diagnosticInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'diagnostic.label', default: 'Diagnostic')] as Object[],
                          "Another user has updated this Diagnostic while you were editing")
                render(view: "editDiagnostic", model: [diagnosticInstance: diagnosticInstance])
                return
            }
        }

        diagnosticInstance.properties = params

        if (!diagnosticInstance.save(flush: true)) {
            render(view: "editDiagnostic", model: [diagnosticInstance: diagnosticInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), diagnosticInstance.id])
        redirect(action: "editDiagnostic", id: diagnosticInstance.id)
    }

    def deleteDiagnostic(Long id) {
        def diagnosticInstance = Diagnostic.get(id)
        if (!diagnosticInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "listDiagnostic")
            return
        }

        try {
            diagnosticInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "listDiagnostic")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'diagnostic.label', default: 'Diagnostic'), id])
            redirect(action: "listDiagnostic", id: id)
        }
    }

    def listService(Long id) {
        def veh=Vehicle.get(id).services;
        [servicesInstanceList: veh, servicesInstanceTotal: Services.count(), carId:id]
    }

    def newService() {
        [servicesInstance: new Services(params)]
    }

    def saveService() {
        def servicesInstance = new Services(params)
        if (!servicesInstance.save(flush: true)) {
            render(view: "newService", model: [servicesInstance: servicesInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'services.label', default: 'Services'), servicesInstance.id])
        redirect(action: "newService", id: servicesInstance.id)
    }
    
    def editService(Long id) {
        def servicesInstance = Services.get(id)
        if (!servicesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "editService")
            return
        }

        [servicesInstance: servicesInstance]
    }

    def updateService(Long id, Long version) {
        def servicesInstance = Services.get(id)
        if (!servicesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "editService")
            return
        }

        if (version != null) {
            if (servicesInstance.version > version) {
                servicesInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'services.label', default: 'Services')] as Object[],
                          "Another user has updated this Services while you were editing")
                render(view: "editService", model: [servicesInstance: servicesInstance])
                return
            }
        }

        servicesInstance.properties = params

        if (!servicesInstance.save(flush: true)) {
            render(view: "editService", model: [servicesInstance: servicesInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'services.label', default: 'Services'), servicesInstance.id])
        redirect(action: "editService", id: servicesInstance.id)
    }

    def deleteService(Long id) {
        def servicesInstance = Services.get(id)
        if (!servicesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "listService")
            return
        }

        try {
            servicesInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "listService")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'services.label', default: 'Services'), id])
            redirect(action: "listService", id: id)
        }
    }
    def newInventory(){
        [inventoryInstance:new Inventory()]
    }
    
    def listInventory(Long id) {
        def veh=Vehicle.get(id).inventory;
        [inventoryInstanceList: veh, inventoryInstanceTotal: Inventory.count(),carId:id]
    }
    
    def saveInventory(){
        def inventoryInstance = new Inventory(params)
        if (!inventoryInstance.save(flush: true)) {
            render(view: "newInventory", model: [inventoryInstance: inventoryInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'inventory.label', default: 'Inventory'), inventoryInstance.id])
        redirect(action: "newInventory", id: inventoryInstance.id)
    }
    
    def editInventory(Long id){
        def inventoryInstance = Inventory.get(id)
        if (!inventoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "editInventory")
            return
        }

        [inventoryInstance: inventoryInstance]
    }
    
    def deleteInventory(Long id) {
        def inventoryInstance = Inventory.get(id)
        if (!inventoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "listInventory")
            return
        }

        try {
            inventoryInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "listInventory")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "listInventory", id: id)
        }
    }
    
    def showInventory(Long id) {
        def inventoryInstance = Inventory.get(id)
        if (!inventoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "editInventory")
            return
        }

        [inventoryInstance: inventoryInstance]
    }
    
    def updateInventory(Long id, Long version) {
        def inventoryInstance = Inventory.get(id)
        if (!inventoryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'inventory.label', default: 'Inventory'), id])
            redirect(action: "editInventory")
            return
        }

        if (version != null) {
            if (inventoryInstance.version > version) {
                inventoryInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'inventory.label', default: 'Inventory')] as Object[],
                          "Another user has updated this Inventory while you were editing")
                render(view: "editInventory", model: [inventoryInstance: inventoryInstance])
                return
            }
        }

        inventoryInstance.properties = params

        if (!inventoryInstance.save(flush: true)) {
            render(view: "editInventory", model: [inventoryInstance: inventoryInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'inventory.label', default: 'Inventory'), inventoryInstance.id])
        redirect(action: "editInventory", id: inventoryInstance.id)
    }
    
}
